package org.ch02.job.test11;

import java.lang.reflect.Field;

/**
 * @author XCH
 * @date 2025 - 03 - 18
 */
public class SqlGenerate {
    public static String generateInsert(Class<?> clz){
        // 获取表名
        String tableName = clz.getSimpleName();
        // 获取字段
        Field[] fields = clz.getDeclaredFields();
        // 拼接sql

        StringBuilder sql = new StringBuilder("insert into ");
        // 遍历字段
        sql.append(tableName+"(");

        for (Field field : fields){
            sql.append(field.getName()).append(",");
        }
        if (sql.length() > 0){
            sql.deleteCharAt(sql.length() - 1);
        }
        sql.append(") values (");
        for (Field field : fields){

            sql.append("?,");
        }

        // 删除最后一个逗号
        if (sql.length() > 0){
            sql.deleteCharAt(sql.length() - 1);
        }
        sql.append(")");

        return sql.toString();
    }

    public static void main(String[] args) {
        System.out.println(generateInsert(User.class));
    }
}
